#include "crectandpos.h"
#include <QGlobalStatic>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QScreen>
#include <QDebug>

Q_GLOBAL_STATIC(CRectAndPos,m_instance)

CRectAndPos::CRectAndPos()
{
    initDialog();
}

CRectAndPos *CRectAndPos::Instance()
{
    return m_instance;
}

void CRectAndPos::SetCurrentData(const QRectF &rect)
{
    label_pos_x->show();
    label_pos_y->show();
    lineedit_pos_x->show();
    lineedit_pos_y->show();

    lineedit_width->setText(QString::number(rect.width(),'f',2));
    lineedit_height->setText(QString::number(rect.height(),'f',2));
    lineedit_pos_x->setText(QString::number(rect.x(),'f',2));
    lineedit_pos_y->setText(QString::number(rect.y(),'f',2));
}

void CRectAndPos::ChangeCurrentData(QRectF &rect)
{    
    rect.setX(lineedit_pos_x->text().toDouble());
    rect.setY(lineedit_pos_y->text().toDouble());
    rect.setWidth(lineedit_width->text().toDouble());
    rect.setHeight(lineedit_height->text().toDouble());
}

void CRectAndPos::SetCurrentData2(qreal width, qreal height)
{
    label_pos_x->hide();
    label_pos_y->hide();
    lineedit_pos_x->hide();
    lineedit_pos_y->hide();
    lineedit_width->setText(QString::number(width,'f',2));
    lineedit_height->setText(QString::number(height,'f',2));
}

void CRectAndPos::ChangeCurrentData2(qreal &width, qreal &height)
{
    width = lineedit_width->text().toDouble();
    height = lineedit_height->text().toDouble();
}

void CRectAndPos::showEvent(QShowEvent *event)
{
    auto dlgSize = this->size();
    const qreal scale = window()->screen()->devicePixelRatio();
    const auto screenSize = window()->screen()->size();

    dlgSize = dlgSize * scale;
    qDebug() << "....scale" << scale;

    if (dlgSize.width() > screenSize.width() || dlgSize.height() >= screenSize.height()) {
        // setFixedWidth(screenSize.width() / scale);
        // setFixedHeight(screenSize.height() / scale);
        // showMaximized();
        showFullScreen();
    }

    QDialog::showEvent(event);
}

void CRectAndPos::initDialog()
{
    btn_ok = new QPushButton("确认");
    connect(btn_ok,&QPushButton::clicked,this,[=](){
        accept();
    });
    btn_cancle = new QPushButton("取消");
    connect(btn_cancle,&QPushButton::clicked,this,[=](){
       reject();
    });

    h_layout_btn = new QHBoxLayout();
    h_layout_btn->addWidget(btn_ok);
    h_layout_btn->addWidget(btn_cancle);

    h_layout_width = new QHBoxLayout();
    label_width = new QLabel("宽度：",this);
    lineedit_width = new QLineEdit(this);
    h_layout_width->addWidget(label_width);
    h_layout_width->addSpacing(10);
    h_layout_width->addWidget(lineedit_width);

    h_layout_height = new QHBoxLayout();
    label_height = new QLabel("高度：",this);
    lineedit_height = new QLineEdit(this);
    h_layout_height->addWidget(label_height);
    h_layout_height->addSpacing(10);
    h_layout_height->addWidget(lineedit_height);

    h_layout_pos_x = new QHBoxLayout();
    label_pos_x = new QLabel("坐标X：",this);
    lineedit_pos_x = new QLineEdit(this);
    lineedit_pos_x->setPlaceholderText("如，-50：向左移动50像素，50：向右移动50像素");
    h_layout_pos_x->addWidget(label_pos_x);
    h_layout_pos_x->addSpacing(10);
    h_layout_pos_x->addWidget(lineedit_pos_x);

    h_layout_pos_y = new QHBoxLayout();
    label_pos_y = new QLabel("坐标Y：",this);
    lineedit_pos_y = new QLineEdit(this);
    lineedit_pos_y->setPlaceholderText("如，-50：向上移动50像素，50：向下移动50像素");
    h_layout_pos_y->addWidget(label_pos_y);
    h_layout_pos_y->addSpacing(10);
    h_layout_pos_y->addWidget(lineedit_pos_y);

    v_layout = new QVBoxLayout(this);
    v_layout->addLayout(h_layout_width);
    v_layout->addSpacing(10);
    v_layout->addLayout(h_layout_height);
    v_layout->addSpacing(10);
    v_layout->addLayout(h_layout_pos_x);
    v_layout->addSpacing(10);
    v_layout->addLayout(h_layout_pos_y);
    v_layout->addSpacing(10);
    v_layout->addLayout(h_layout_btn);
    v_layout->addSpacing(10);

    setLayout(v_layout);

    resize(400,400);
}
